package cn.wolfcode.wolf2w.advice;

import cn.wolfcode.wolf2w.exception.LoginException;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by 甘静斌 on 2020-08-08 19:32
 */

@ControllerAdvice
public class CommonExceptionHandler {

    //这个方法定义的根映射方法操作一样
    @ExceptionHandler(LoginException.class)
    @ResponseBody
    public Object loginExp(Exception e, HttpServletResponse resp){

        e.printStackTrace();
        resp.setContentType("application/json;charset=utf-8");

        return JsonResult.error(JsonResult.CODE_ERROR_PARAM,e.getMessage() ,null );
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Object runTimeExp(Exception e, HttpServletResponse resp){

        e.printStackTrace();
        resp.setContentType("application/json;charset=utf-8");

        return JsonResult.defaultError();
    }
}
